package base.stages
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class StageLayerMask extends Sprite
	{
		private var _maskType:int;
		
		public function StageLayerMask(maskType:int)
		{
			_maskType=maskType;
			StageReflection.stage.addEventListener(Event.RESIZE, __onMaskShapeResize);
			addEventListener(MouseEvent.MOUSE_DOWN, __onMaskShapeMouseEvent);
			addEventListener(MouseEvent.CLICK, __onMaskShapeMouseEvent);
			addEventListener(MouseEvent.DOUBLE_CLICK, __onMaskShapeMouseEvent);
			drawMask();
		}
		
		private function drawMask():void
		{
			graphics.clear();
			switch(_maskType)
			{
				case StageLayer.ALPHA_MASK:
					graphics.beginFill(0x000000, 0);
					break;
				case StageLayer.GRAY_MASK:
					graphics.beginFill(0x000000, 0.5);
					break;
				case StageLayer.BLACK_MASK:
					graphics.beginFill(0x000000);
					break;				
			}
			graphics.drawRect(0,0,StageReflection.stageWidth, StageReflection.stageHeight);
			graphics.endFill();
		}
		
		private function __onMaskShapeResize(evt:Event):void
		{
			drawMask();
		}
		
		private function __onMaskShapeMouseEvent(evt:MouseEvent):void
		{
			evt.stopImmediatePropagation();
		}
		
		public function dispose():void
		{
			graphics.clear();
			StageReflection.stage.removeEventListener(Event.RESIZE, __onMaskShapeResize);
			removeEventListener(MouseEvent.MOUSE_DOWN, __onMaskShapeMouseEvent);
			removeEventListener(MouseEvent.CLICK, __onMaskShapeMouseEvent);
			removeEventListener(MouseEvent.DOUBLE_CLICK, __onMaskShapeMouseEvent);
			if(parent)parent.removeChild(this);
		}
	}
}